package org.comidas;

import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsSpinner;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Scroller;

public class AdicionarComida extends Activity implements OnClickListener,  ShutterCallback, PictureCallback{

	protected AbsSpinner horas, minutos, segundos;
	protected EditText nomeComida, descricaoComida;
	protected ListaComidas listaComidas = ListaComidas.getInstance();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.adicionarcomida);

		Scroller s = new Scroller(this);

		nomeComida = (EditText) findViewById(R.id.nomeAlimento);
		descricaoComida = (EditText) findViewById(R.id.descricaoAlimento);

		addSpinners();

		View saveButton = findViewById(R.id.ok);
		saveButton.setOnClickListener(this);
		

		
		View botaoTirarFoto = findViewById(R.id.botaoAbrirCamera);
		botaoTirarFoto.setOnClickListener(this);

		View botaoRemoverFoto = findViewById(R.id.botaoRemoverImagem);
		botaoRemoverFoto.setOnClickListener(this);

		View botaoBuscarImagem = findViewById(R.id.botaoBuscarImagem);
		botaoBuscarImagem.setOnClickListener(this);

		
	}

	protected void addSpinners() {
		horas = (AbsSpinner) findViewById(R.id.numeroHoras);
		minutos = (AbsSpinner) findViewById(R.id.numeroMinutos);
		segundos = (AbsSpinner) findViewById(R.id.numeroSegundos);

		ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
				R.array.minutos, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		horas.setAdapter(adapter);
		minutos.setAdapter(adapter);
		segundos.setAdapter(adapter);

	}

	public String getNomeComida() {
		return nomeComida.getText().toString();
	}

	public String getDescricaoComida() {
		return descricaoComida.getText().toString();
	}

	protected int getHoras() {
		return Integer.valueOf(horas.getSelectedItem().toString());
	}

	protected int getMinutos() {
		return Integer.valueOf(minutos.getSelectedItem().toString());
	}

	protected int getSegundos() {
		return Integer.valueOf(segundos.getSelectedItem().toString());
	}

	public void onClick(View v) {
		ImageView image = (ImageView) findViewById(R.id.imagemAlimento);
		
		switch (v.getId()) {
		case R.id.botaoBuscarImagem:
			break;
		case R.id.botaoAbrirCamera:
			Intent cameraUse = new Intent(this, CameraUse.class);
			startActivity(cameraUse);
			break;
			
		case R.id.botaoRemoverImagem:
			break;
		case R.id.ok:
			String novaComidaStr = getNomeComida();
			int indexAAdicionar = listaComidas.contains(novaComidaStr);
			if (novaComidaStr.trim().equals("") || indexAAdicionar != -1
					|| novaComidaStr.trim().equals("Nome")) {
				startActivity(new Intent(this, AvisoInvalidez.class));
			} else {
				int[] tempo = { getHoras(), getMinutos(), getSegundos() };
				Comida novaComida = new Comida(novaComidaStr, tempo,
						getDescricaoComida(), " ");
				listaComidas.addComida(novaComida);
				finish();
			}
			break;
		}
	}

	public void onPictureTaken(byte[] data, Camera camera) {
		// TODO Auto-generated method stub
		
	}

	public void onShutter() {
		// TODO Auto-generated method stub
		
	}
}